package demo;
 
import java.util.ArrayList;
import java.util.List;

import org.bson.Document;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
 
public class MainDriver {
    
     private MongoClient mongo = null;
    
     private MongoDatabase db = null;
    
     @Before
     public void init(){
         //创建连接
         mongo = new MongoClient("127.0.0.1", 27017);
         //获取数据库
         db = mongo.getDatabase("mydb");
     }
    
     public void getColonyMongo(){
			ServerAddress server_1 = new ServerAddress("192.168.137.3", 27017);
			ServerAddress server_2 = new ServerAddress("192.168.137.4", 27017);
			ServerAddress server_3 = new ServerAddress("192.168.137.5", 27017);
			
			List<ServerAddress> serverAddressList = new ArrayList<ServerAddress>();
			
			serverAddressList.add(server_1);
			serverAddressList.add(server_2);
			serverAddressList.add(server_3);
			
			MongoClient client = new MongoClient(serverAddressList);
     }
     
     @After
     public void destory(){
         //关闭连接
         mongo.close();
     }
    
     //创建集合
     @Test
     public void create(){
         db.createCollection("1506C");
     }
    
     /**
      * 查询
      */
     @Test
     public void query(){
         //查询所有
         System.out.println("-----查询所有-------");
         MongoCursor<Document> cursor = db.getCollection("stus").find().iterator();
         while(cursor.hasNext()){
              Document document = cursor.next();
              System.out.println(document);
         }
         System.out.println("------条件查询-------");
         //条件查询
         MongoCursor<Document> iterator = db.getCollection("stus").find(Filters.and(Filters.gt("age", 23),Filters.lt("age", 26))).iterator();
         while(iterator.hasNext()){
              Document document = iterator.next();
              System.out.println(document);
         }
        
     }
    
     /**
      * 新增
      */
     @Test
     public void add(){
         MongoCollection<Document> collection = db.getCollection("stu");
         Document d = new Document();
         d.put("_id", 100);
         d.put("name", "孙悟空");
         d.put("age", 500);
         //插入一个文档
         collection.insertOne(d);
        
         Document d2 = new Document();
         d2.put("_id", 101);
         d2.put("name", "猪八戒");
         d2.put("age", 501);
        
         Document d3 = new Document();
         d3.put("_id", 102);
         d3.put("name", "沙悟净");
         d3.put("age", 503);
         List<Document> ll = new ArrayList<>();
         ll.add(d2);
         ll.add(d3);
        
         //插入多个文档
         collection.insertMany(ll);
     }
    
     /**
      * 删除
      */
     @Test
     public void delete(){
         MongoCollection<Document> collection = db.getCollection("stu");
         //删除多个
         collection.deleteMany(Filters.lt("age", 502));
//       collection.deleteOne() //删除一个
     }
    
     /**
      * 修改
      */
     @Test
     public void update(){
         MongoCollection<Document> collection = db.getCollection("stu");
         collection.updateMany(Filters.lt("age",900), new Document("$set",new Document("desci","西游记")));
     }
    
}

